/*
 * bluetoothReader.java
 *
 * Created on 18 de septiembre de 2007, 21:42
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package project;

import java.io.*;
import java.util.Date;
import javax.microedition.io.*;
/**
 *
 * @author Ana_Pedro
 */
public class bluetoothReader extends Thread {
    
    String btURL;
    volatile boolean continuarBt;
    
    private String lineaGPRMC;
    private String lineaGPGGA;
    private boolean flagGPRMC=false;
    private boolean flagGPGGA=false;
    private volatile boolean state=true;
    
    /** Creates a new instance of bluetoothReader */
    public bluetoothReader(String str) {
        btURL=str;
    }
    
    public synchronized void setGPRMC(String str) {
        lineaGPRMC=str;
        flagGPRMC=true;
    }
    
    public synchronized void setGPGGA(String str) {
        lineaGPGGA=str;
        flagGPGGA=true;
    }
    
    public synchronized String getGPRMC() {
        if(flagGPRMC) {
            flagGPRMC=false;
            return(lineaGPRMC);
        } else
            return(null);
            
    }
    
    public synchronized String getGPGGA() {
        if(flagGPGGA) {
            flagGPGGA=false;
            return(lineaGPGGA);
        } else 
            return(null);
    }
    
    public void pararBt() {
        continuarBt=false;
    }
    
    public synchronized void setState(boolean estado) {
        state=estado;
    }
    
    public synchronized boolean getState() {
        return(state);
    }

    public void run() {
        
        int c=0;
        int b1, b2;
        String linea;
        
        StreamConnection connection=null;
        InputStream is=null;
        
        continuarBt=true;
        
        long readTimeout=0L;
        
        int checksum=0;
        int l;
        
        try {
            connection=(StreamConnection)Connector.open(btURL);
            is=connection.openInputStream();
        } catch(IOException e) {  
        }

            while(continuarBt) {
                        linea="";

                        try {                           
                            while(true) {
                                synchronized(this) {
                                    l=is.available();
                                }
                                if(l==0) {
                                    readTimeout=System.currentTimeMillis();
                                    do {
                                        if(System.currentTimeMillis() - readTimeout > 5000)
                                            throw new Exception();
                                        synchronized(this) {
                                            l=is.available();
                                        }
                                    } while(l==0);
                                    
                                    Thread.yield();
                                    
                                }
                                
                                while(l>0) {
                                    synchronized(this) {
                                        c=is.read();
                                    }
                                    if(c!='\r')
                                        linea+=String.valueOf((char)c);
                                    else
                                        break;
                                    l--;
                                }
                                
                                if(l>0)
                                    break;
                                
                            }
                        } catch(Exception e1) {
                            setState(false);
                            try {
                                if(is!=null)
                                    is.close();
                                if(connection!=null)
                                    connection.close();
                                is=null;
                                connection=null;
                                
                                Thread.sleep(1000);

                                connection=(StreamConnection)Connector.open(btURL);
                                is=connection.openDataInputStream();
                            } catch(Exception e2) {
                                is=null;
                                connection=null;
                            }
                            continue;
                        }
                  
                        try {
                            setState(true);
                            
                            if((b1=linea.indexOf("$GPRMC"))!=-1) {
                                b2=linea.indexOf('*');
                                checksum=0;
                                for(int i=b1+1; i<b2; i++)
                                    checksum^=linea.charAt(i);
                                
                                try {
                                    if(checksum!=Integer.parseInt(linea.substring(b2+1),16))
                                        continue;
                                } catch(Exception e) {
                                    continue;
                                }
                                setGPRMC(linea);
                            } else if((b1=linea.indexOf("$GPGGA"))!=-1) {
                                b2=linea.indexOf('*');
                                checksum=0;
                                for(int i=b1+1;i<b2;i++)
                                    checksum^=linea.charAt(i);
                                
                                try {
                                    if(checksum!=Integer.parseInt(linea.substring(b2+1),16))
                                        continue;
                                } catch(Exception e) {
                                    continue;
                                }
                                setGPGGA(linea);
                            }
                        
                        } catch(Exception e) {}
            }
            
        
        try {
            if(is!=null)
                is.close();
            if(connection!=null)
                connection.close();
        } catch(IOException e) {
            
        }
    }
    
}
